package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.Ordertime;
import cn.wolfcode.p2p.base.domain.Videoauth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.excetion.DisplayableExcetion;
import cn.wolfcode.p2p.base.mapper.VideoauthMapper;
import cn.wolfcode.p2p.base.query.QueryObjext;
import cn.wolfcode.p2p.base.service.ILofinInfoService;
import cn.wolfcode.p2p.base.service.IOrdertimeService;
import cn.wolfcode.p2p.base.service.IVideoauthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.BitStateUtil;
import cn.wolfcode.p2p.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class VideoauthServiceImpl implements IVideoauthService {

    @Autowired
    private VideoauthMapper videoAuthMapper;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ILofinInfoService lofinInfoService;
    @Autowired
    private IOrdertimeService ordertimeService;

    @Override
    public int insert(Videoauth record) {
        return videoAuthMapper.insert(record);
    }

    @Override
    public int update(Videoauth record) {
        return videoAuthMapper.updateByPrimaryKey(record);
    }

    @Override
    public Videoauth get(Long id) {
        return videoAuthMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo queryPage(QueryObjext qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List list = videoAuthMapper.selectList(qo);
        return new PageInfo(list);
    }


    /**
     * 视频预约
     * @param timeid    那一天的哪个时间段,预约时间
     * @param orderDate 预约日期
     * @param auditorId 预约客服
     */
    public void apply(Long timeid, String orderDate, Long auditorId) {
        //校验参数
        Ordertime ordertime = ordertimeService.get(timeid);//时间段
        if (ordertime == null) {
            throw new DisplayableExcetion("非法参数");
        }
        LoginInfo auditor = lofinInfoService.get(auditorId);//客服
        if (auditor == null) {
            throw new DisplayableExcetion("非法参数");
        }
        if (auditor.getIsAuditor() != LoginInfo.ISAUTITOR_YES) {
            throw new DisplayableExcetion("非法参数");
        }
        Videoauth videoauth = new Videoauth();
        videoauth.setApplier(UserContext.getCount());
        videoauth.setAuditor(auditor);
        videoauth.setApplyTime(new Date());
        videoauth.setState(Videoauth.STATE_NORMAL);
        String oderBeginDate = orderDate + " " + ordertime.getBegin();
        String oderEndDate = orderDate + " " + ordertime.getEnd();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            videoauth.setOrderBeginDate(sdf.parse(oderBeginDate));
            videoauth.setOrderEndDate(sdf.parse(oderEndDate));
        } catch (ParseException e) {
            e.printStackTrace();
            throw new DisplayableExcetion("非法参数");
        }
        this.insert(videoauth);
        UserInfo userInfo = userInfoService.getCurrent();
        userInfo.setVideoAuthId(videoauth.getId());
        userInfoService.updateByPrimaryKey(userInfo);
    }

    //后台审核
    @Override
    public void audit(Integer state, Long videoAuthId, String remark) {
        Videoauth videoauth = this.get(videoAuthId);
        if (videoauth != null &&videoauth.getState() == Videoauth.STATE_NORMAL ) {
            videoauth.setAuditTime(new Date());
            videoauth.setRemark(remark);
            UserInfo userInfo = userInfoService.get(videoauth.getApplier().getId());
            if(state == Videoauth.STATE_PASS){
                videoauth.setState(Videoauth.STATE_PASS);
                //给用户添加视频认证状态码
                userInfo.addState(BitStateUtil.HAS_VIDEO_AUTH);
            }else {
                videoauth.setState(Videoauth.STATE_REJECT);
                userInfo.setVideoAuthId(null);
            }
            userInfoService.updateByPrimaryKey(userInfo);
            this.update(videoauth);
        }
    }
}
